package leetcode100;

import java.util.ArrayList;
import java.util.List;

public class lc036 {
    public static void main(String[] args) {
        int[] nums = {1, 2, 3};

        List<List<Integer>> res = new lc036().permute(nums);

        System.out.println(res);
    }

    public List<List<Integer>> permute(int[] nums) {

        List<List<Integer>> res = new ArrayList<>();
        boolean[] used = new boolean[nums.length];
        backtrace(res, new ArrayList<>(), used, nums);
        return res;
    }

    public void backtrace(List<List<Integer>> res, List<Integer> current, boolean[] used, int[] nums) {
        if (current.size() == nums.length) {
            res.add(new ArrayList<>(current));
            return;
        }

        for (int i = 0; i < nums.length; i++) {
            if (used[i]) {
                continue;
            }
            current.add(nums[i]);
            used[i] = true;
            backtrace(res, current, used, nums);
            current.remove(current.size() - 1);
            used[i] = false;
        }
    }
}
